#include "configprocess.h"
#include <QtXml>
#include <QDomElement>
#include <QMessageBox>
#include <QDebug>
#include <QApplication>

QConfigProcess::QConfigProcess(QObject *parent) : QObject(parent)
{
     m_sDevConfigPath = QApplication::applicationDirPath()+"/config/devconfig.xml";
     m_sAttrConfigPath = QApplication::applicationDirPath()+"/config/attrconfig";

}

//获取设备配置信息
bool QConfigProcess::getDevConfig()
{
    QDomDocument configDoc;
    QFile file(m_sDevConfigPath);
    if(!file.open(QIODevice::ReadOnly))
    {
        return false;
    }

    if(!configDoc.setContent(&file))
    {
        file.close();
        return false;
    }


    QDomElement root = configDoc.documentElement();

    QDomNodeList nodelist = root.childNodes();
    for(int i =0;i < nodelist.count();i++)
    {
        QDomNode subitem = nodelist.at(i);

        if(subitem.nodeName() == "Item")
        {
            tDevConfigInfo l_tempConfig;

            int index = subitem.attributes().namedItem("Index").nodeValue().toInt();
            l_tempConfig.sIPAddress = subitem.attributes().namedItem("Addr").nodeValue();
            l_tempConfig.sDevName = subitem.attributes().namedItem("Name").nodeValue();
            l_tempConfig.sSendPort = subitem.attributes().namedItem("SendPort").nodeValue();
            l_tempConfig.sRcvPort = subitem.attributes().namedItem("RcvPort").nodeValue();

            m_tDevConfigParamMap[index] = l_tempConfig;
        }
    }
    file.close();

    return true;

}

bool QConfigProcess::getAttrConfig()
{

    QFile l_file(m_sAttrConfigPath);
    if(l_file.open(QFile::ReadOnly) == false)
    {
        return false;
    }

    QByteArray l_data= l_file.readAll();


    QJsonParseError error;
    QJsonDocument document = QJsonDocument::fromJson(l_data,&error);
    if(error.error != QJsonParseError::NoError)
    {
        qInfo()<<"parse json error"<<error.errorString();
        return false;
    }

    if(document.isNull()||document.isEmpty())
    {
        qInfo()<<"parse json null or empty";
        return false;
    }

    QJsonArray grouplist = document.array();

    foreach (auto Item, grouplist)
    {
       QJsonObject item_obj = Item.toObject();

       st_parmGroup l_st_parmGroup;
       l_st_parmGroup.sGroupName = item_obj.value("GoupName").toString();

       QJsonValue l_cmd_item = item_obj.value("Item");
       QJsonArray l_cmd_list = l_cmd_item.toArray();

       foreach (auto cmdItem, l_cmd_list)
       {
           QJsonObject cmdItem_obj = cmdItem.toObject();

           st_paramInfo l_st_paramInfo;
           l_st_paramInfo.sParamName = cmdItem_obj.value("name").toString();
           l_st_paramInfo.i32MinValue = cmdItem_obj.value("minvalue").toString().toInt();
           l_st_paramInfo.i32MaxValue = cmdItem_obj.value("maxvalue").toString().toInt();
           l_st_paramInfo.i32CmdId = cmdItem_obj.value("cmdid").toString().toInt();

           if(!cmdItem_obj["select"].isNull())
           {
               QJsonValue l_select_item = cmdItem_obj.value("select");
               QJsonArray l_select_list = l_select_item.toArray();
               foreach (auto select_item, l_select_list)
               {
                   QJsonObject select_item_obj = select_item.toObject();

                   st_selectItem l_st_selectItem;
                   l_st_selectItem.sName = select_item_obj.value("name").toString();
                   l_st_selectItem.i32Value = select_item_obj.value("value").toString().toInt();

                   l_st_paramInfo.selectlist.append(l_st_selectItem);
               }

           }
           l_st_parmGroup.paramlist.append(l_st_paramInfo);
       }
        m_st_parmGroup.append(l_st_parmGroup);
    }
    return true;
}
